package com.ctyon.watchmanager.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.Editable;
import android.widget.EditText;
import android.widget.Toast;

import com.ctyon.common.util.DataUtil;
import com.ctyon.watchmanager.R;

import androidx.annotation.NonNull;

/**
 * CreateDate：19-7-22 on 上午10:01
 * Describe:
 * Coder: lee
 */
public class EditPasswordDialog extends Dialog {

    private PasswordDialogCallback passwordDialogCallback;
    private EditText etPasswordOld, etPasswordNew, etPasswordNew2;
    private Context context;

    public EditPasswordDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
        this.context = context;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_password);

        etPasswordOld = findViewById(R.id.dialog_password_old);
        etPasswordNew = findViewById(R.id.dialog_password_new);
        etPasswordNew2 = findViewById(R.id.dialog_password_new2);

        findViewById(R.id.dialog_cancel).setOnClickListener(v -> {
            cancel();
        });
        findViewById(R.id.dialog_submit).setOnClickListener(v -> {
            if (null != passwordDialogCallback) {
                Editable po = etPasswordOld.getText();
                Editable pn = etPasswordNew.getText();
                Editable pn2 = etPasswordNew2.getText();
                if (DataUtil.isNullOr(po, pn, pn2)) {
                    Toast.makeText(context, "输入有误,不能为空", Toast.LENGTH_SHORT).show();
                    return;
                }
                String pos = po.toString();
                String pns = pn.toString();
                String pn2s = pn2.toString();
                if (DataUtil.isEmptyOr(pos, pns, pn2s)) {
                    Toast.makeText(context, "输入有误,不能为空", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (pns.equals(pos)){
                    Toast.makeText(context, "跟旧密码相同,无修改", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (!pns.equals(pn2s)){
                    Toast.makeText(context, "重复新密码错误", Toast.LENGTH_SHORT).show();
                    return;
                }
                passwordDialogCallback.editCallback(pos, pns);
            }
            cancel();
        });
    }

    public interface PasswordDialogCallback {
        void editCallback(String po, String pn);
    }

    public void setEditCallback(PasswordDialogCallback passwordDialogCallback) {
        this.passwordDialogCallback = passwordDialogCallback;
    }

}
